package com.ziyun.pop.rest.module.mall.address;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.service.order.api.AddressService;
import com.service.order.constant.OrderResult;
import com.service.order.model.address.AddressParamDto;
import com.service.order.model.address.AddressResultDto;
import com.ziyun.pop.common.utils.UserUtils;

import io.renren.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value = "AddressRestApi", tags = "商城地址服务")
@RestController
@RequestMapping("popApi/mall/address/")
public class AddressRestApi {

	@Autowired
	private AddressService addressService;

	@ApiOperation(value = "获取用户所有地址")
	@ApiResponses(value = @ApiResponse(code = 200, message = "获取用户所有地址", response = AddressResultDto.class))
	@PostMapping("/listAddressInfo")
	public R listAddressInfo() {
		OrderResult<List<AddressResultDto>> addressResult = addressService.listAddressInfo(UserUtils.getMallUserId());
		if (R.isError(addressResult.getCode())) {
			return R.error(addressResult.getCode(), addressResult.getMessage());
		}
		return R.apiOk(addressResult.getData());
	}

	@ApiOperation(value = "根据地址id获取地址信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "addressId", value = "地址id", required = true, paramType = "query", dataType = "Integer") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "获取地址详情", response = AddressResultDto.class))
	@GetMapping(value = "getAddressInfoById")
	public R getAddressInfoById(@RequestParam("addressId") Integer addressId) {
		OrderResult<AddressResultDto> addressResult = addressService.getAddressInfoById(UserUtils.getMallUserId(),
				addressId);
		if (R.isError(addressResult.getCode())) {
			return R.error(addressResult.getCode(), addressResult.getMessage());
		}
		return R.apiOk(addressResult.getData());
	}

	@ApiOperation(value = "新增或修改地址")
	@ApiResponses(value = @ApiResponse(code = 200, message = "新增或修改地址成功", response = AddressResultDto.class))
	@PostMapping("/saveOrUpdateAddress")
	public R saveOrUpdateAddress(@RequestBody AddressParamDto addressParamDto) {
		OrderResult<AddressResultDto> addressResult = addressService.saveOrUpdateAddress(UserUtils.getMallUserId(),
				addressParamDto);
		if (R.isError(addressResult.getCode())) {
			return R.error(addressResult.getCode(), addressResult.getMessage());
		}
		return R.apiOk(addressResult.getData());
	}

	@ApiOperation(value = "删除地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "addressId", value = "地址id", required = true, paramType = "query", dataType = "Integer") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "删除地址"))
	@PostMapping("/removeAddress")
	public R removeAddress(@RequestBody Map<String, Object> map) {
		OrderResult<?> addressResult = addressService.removeAddress(UserUtils.getMallUserId(), map);
		if (R.isError(addressResult.getCode())) {
			return R.error(addressResult.getCode(), addressResult.getMessage());
		}
		return R.apiOk();
	}

	@ApiOperation(value = "设置默认地址")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "addressId", value = "地址id", required = true, paramType = "query", dataType = "Integer") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "设置默认地址成功"))
	@PostMapping("/updateDefAddress")
	public R updateDefAddress(@RequestBody Map<String, Object> map) {
		OrderResult<?> addressResult = addressService.updateDefAddress(UserUtils.getMallUserId(), map);
		if (R.isError(addressResult.getCode())) {
			return R.error(addressResult.getCode(), addressResult.getMessage());
		}
		return R.apiOk();
	}

}
